package com.holdtime.jtt809.taxi.module.service.impl;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import com.holdtime.jtt809.taxi.module.entity.DmsAlarmDO;
import com.holdtime.jtt809.taxi.module.entity.mapper.DmsAlarmMapper;
import com.holdtime.jtt809.taxi.module.model.vo.DmsAlarmPageReqVO;
import com.holdtime.jtt809.taxi.module.model.vo.DmsAlarmSaveReqVO;
import com.holdtime.jtt809.taxi.module.service.DmsAlarmService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.holdtime.jtt809.taxi.module.enums.Taxi809ErrorCodeConstants.DMS_ALARM_NOT_EXISTS;

/**
 * dms报警记录 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class DmsAlarmServiceImpl implements DmsAlarmService {

    @Resource
    private DmsAlarmMapper dmsAlarmMapper;

    @Override
    public String createDmsAlarm(DmsAlarmSaveReqVO createReqVO) {
        // 插入
        DmsAlarmDO dmsAlarm = BeanUtils.toBean(createReqVO, DmsAlarmDO.class);
        dmsAlarmMapper.insert(dmsAlarm);
        // 返回
        return dmsAlarm.getId();
    }

    @Override
    public void updateDmsAlarm(DmsAlarmSaveReqVO updateReqVO) {
        // 校验存在
        validateDmsAlarmExists(updateReqVO.getId());
        // 更新
        DmsAlarmDO updateObj = BeanUtils.toBean(updateReqVO, DmsAlarmDO.class);
        dmsAlarmMapper.updateById(updateObj);
    }

    @Override
    public void deleteDmsAlarm(String id) {
        // 校验存在
        validateDmsAlarmExists(id);
        // 删除
        dmsAlarmMapper.deleteById(id);
    }

    private void validateDmsAlarmExists(String id) {
        if (dmsAlarmMapper.selectById(id) == null) {
            throw exception(DMS_ALARM_NOT_EXISTS);
        }
    }

    @Override
    public DmsAlarmDO getDmsAlarm(String id) {
        return dmsAlarmMapper.selectById(id);
    }

    @Override
    public PageResult<DmsAlarmDO> getDmsAlarmPage(DmsAlarmPageReqVO pageReqVO) {
        return dmsAlarmMapper.selectPage(pageReqVO);
    }

}